/**
 * 产品管理js
 */
var productInfo = {
	curPage : 1,
	pageSize : 10,
	dataCache : null,

	startEvent : function() {
		// 按回车键登录事件
		document.onkeydown = function(e) {
			var ev = document.all ? window.event : e;
			if (ev.keyCode == 13) {
				productInfo.curPage = 1;
				productInfo.setPageChange(null);
				productInfo.search();
			}
		};
		
		$('.subType').click(function() {
			alert($(this).attr("id"));
		});	
	},
	
	bindEvent : function() {
		// 查询
		$('#searchData').click(function() {
			productInfo.curPage = 1;
			productInfo.setPageChange(null);
			productInfo.search();
		});

		$('#addData').on('click', function() {
			var path = APP_ADDRESS + 'mgr/product/productType05/productInfoAdd.jsp';
			art.dialog.open(path, {
				title : '添加产品',
				width : 900,
				height : 540,
				ok : function() {
					var returnStatus = this.iframe.contentWindow.submitForm();
					if (returnStatus) {
						productInfo.setPageChange(null);
						productInfo.search();
					}
					return false;
				},
				cancel : true
			});
		});
		
			
		
	},

	// 设置分页
	setPageChange : function(delCurPage) {
		var url = 'mgr/OverseaSafeInfoCtrl/getOverseaSafeInfoList';
		var prodType = $.trim($('#searchModForm select[name="prodType"]').val());
		var safeCompany = $.trim($('#searchModForm select[name="safeCompany"]').val());
		var name = $.trim($('#searchModForm input[name="name"]').val());
		var params = {
			'curPage' : 1,
			'pageSize' : 99999,
			'safeCompany' : safeCompany,
			'prodType':prodType,
			'name' : name
		};
		var cb = function(data) {
			if (data.resultCode == 200) {
				var totalCount = data.size;
				var pageTotal = (totalCount / productInfo.pageSize > 0) ? Math
						.ceil(totalCount / productInfo.pageSize) : 1;
				pageHelper.init(pageTotal, totalCount, productInfo.search,
						delCurPage); // 翻页事件
			}
		};
		sendApiNoLoad(url, params, cb, 'POST');
	},

	// 查询系统用户
	search : function(pageIdx) {
		var url = 'mgr/OverseaSafeInfoCtrl/getOverseaSafeInfoList';
		if (typeof (pageIdx) != "undefined") {
			productInfo.curPage = pageIdx;
		}
		var prodType = $.trim($('#searchModForm select[name="prodType"]').val());
		var safeCompany = $.trim($('#searchModForm select[name="safeCompany"]').val());
		var name = $.trim($('#searchModForm input[name="name"]').val());
		var params = {
			'curPage' : productInfo.curPage,
			'pageSize' : productInfo.pageSize,
			'safeCompany' : safeCompany,
			'prodType':prodType,
			'name' : name
		};
		var cb = function(data) {
			if (data.resultCode == 200) {
				$('#tableList tbody').empty();
				var tr = '';
				if (data.result != null && data.result.length > 0) {
					for (var i = 0; i < data.result.length; i++) {
						var singleObj = data.result[i];
						tr += '<tr><td><input type="hidden" name="id" value="'
								+ singleObj.id
								+ '"/>'
								+ (i + 1)
								+ '</td><td>'
								+  (singleObj.name==null?'':singleObj.name)
								+ '</td><td>'
								+  jsUtils.getDictionaryTxtByType('safeCompany',singleObj.safeCompany)
								+ '</td><td>'
								+ jsUtils.getDictionaryTxtByType('oprodType',singleObj.prodType)
								+ '</td><td>'
								+  (singleObj.dividendGains==null?'':singleObj.dividendGains)
								+ '</td><td>'
								+ '<a type="button" name="productType05_94" class="btn btn-primary btn-xs lookData">'
								+ '<i class="fa fa-reorder"></i> 查看 </a>'
								+ '<a type="button" name="productType05_96" class="btn btn-success btn-xs updateData" style="margin-left: 5px;display:none;">'
								+ '<i class="fa fa-edit"></i> 编辑</a>'
								+ '<a type="button" name="productType05_100" class="btn btn-success btn-xs copyData" style="margin-left: 5px;display:none;">'
								+ '<i class="fa fa-edit"></i>一键复制</a>'
								+ '<a type="button" name="productType05_97" class="btn btn-default btn-xs deleteData" style="margin-left: 5px;display:none;">'
								+ '<i class="fa fa-remove"></i> 删除</a>'
								+ '</td></tr>';
					}
				} else {
					tr = '<tr><td colspan="7" style="text-align: center;color: red;">暂无数据</td></tr>';
				}
				$('#tableList tbody').html(tr);
				productInfo.bindEventOtherBtn();
			}
			jsUtils.controlFunctionBtn();
		};
		sendApiNoLoad(url, params, cb, 'POST');
	},

	bindEventOtherBtn : function() {
		$('.deleteData').unbind();
		$('.deleteData').on(
				'click',
				function() {
					var thisObj = $(this);
					art.dialog.confirm('你确认删除？', function() {
						var id = thisObj.parent().parent().find('input[name="id"]').eq(0).val();
						var url = 'mgr/OverseaSafeInfoCtrl/deleteOverseaSafeInfo';
						var params = {
							'id' : id
						};
						var cb = function(data) {
							alert('删除成功！');
							productInfo.setPageChange(productInfo.curPage);
							productInfo.search(productInfo.curPage);
						}
						sendApiNoLoad(url, params, cb, 'POST');
					}, function() {
					});
				});
		$('.copyData').unbind();
		$('.copyData').on(
				'click',
				function() {
					var thisObj = $(this);
					art.dialog.confirm('你确认复制？', function() {
						var id = thisObj.parent().parent().find(
								'input[name="id"]').eq(0).val();
						var url = 'mgr/ProductInfoCtrl/copy';
						var params = {
							'id' : id
						};
						var cb = function(data) {
							alert('复制成功！');
							productInfo.setPageChange(productInfo.curPage);
							productInfo.search(productInfo.curPage);
						}
						sendApiNoLoad(url, params, cb, 'POST');
					}, function() {
					});
				});
		$('.updateData').unbind();
		$('.updateData').on(
				'click',
				function() {
					var thisObj = $(this);
					var id = thisObj.parent().parent().find('input[name="id"]')
							.eq(0).val();
					art.dialog.data('updId', id);
					var path = APP_ADDRESS
							+ 'mgr/product/productType05/productInfoUpdate.jsp';
					art.dialog.open(path, {
						title : '修改产品',
						width : 900,
						height : 540,
						ok : function() {
							var returnStatus = this.iframe.contentWindow
									.submitForm();
							if (returnStatus) {
								productInfo.setPageChange(productInfo.curPage);
								productInfo.search(productInfo.curPage);
							}
							return false;
						},
						cancel : true
					});
				});
		$('.lookData').unbind();
		$('.lookData').on(
				'click',
				function() {
					var thisObj = $(this);
					var id = thisObj.parent().parent().find('input[name="id"]')
							.eq(0).val();
					art.dialog.data('lokId', id);
					var path = APP_ADDRESS
							+ 'mgr/product/productType05/productInfoLook.jsp';
					art.dialog.open(path, {
						title : '查看产品',
						width : 900,
						height : 500,
						cancel : true
					});
				});
	},

	init : function() {
		productInfo.setPageChange(null);
		productInfo.search();
		productInfo.startEvent();
		productInfo.bindEvent();
	}
};

$(document).ready(function() {
	productInfo.init();
});

//提供给外部刷新调用
function refashData() {
	productInfo.setPageChange(null);
	productInfo.search();
};

